package com.honestpeak.datafusion.web;


import java.util.List;
import java.util.Map;

import com.honestpeak.datafusion.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.honestpeak.core.datasource.DataSourceContextHolder;
import com.honestpeak.core.datasource.DataSourceType;
import com.honestpeak.datafusion.mapper.ClubMemberInfoMapper;
import com.honestpeak.datafusion.model.Dwh;
import com.honestpeak.datafusion.service.ClubMemberDistService;

/**
 * @Auther: apple
 * @Date: 2018/8/11 11:22
 * @Description: 体育俱乐部控制层
 */
@Controller
@RequestMapping("/sports")
public class SportsController {

	@Autowired
	private ClubMemberInfoMapper clubMemberInfoMapper;
	
	@Autowired
	private ClubMemberDistService clubMemberDistService;
	
    @RequestMapping("/ui")
    public Object UI(){
        return "sports/clubData";
    }
    
    /**
     * 查询俱乐部数据
     * @return
     */
    @RequestMapping(value = "/clubData", method = RequestMethod.GET)
    public ModelAndView clubData(ModelAndView mav){
        //切换数据库
        DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
      //查询学年
        List<String> yearList = clubMemberDistService.getYearTermInfo();
        List<String> clubList = clubMemberDistService.getClubInfo();
        DataSourceContextHolder.clearDbType();
        mav.addObject("yearList", yearList);
        mav.addObject("clubList", clubList);
        mav.setViewName("sports/clubData");
        return mav;
    }
    
    @RequestMapping(value = "/clubMemberNum", method = RequestMethod.GET)
    @ResponseBody
    public Object getclubMemberNum(@RequestParam(value = "year", required = false) String year){
        DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
        Map<String, Object> resultMap = clubMemberDistService.getClubMemberNumByYear(year);
        DataSourceContextHolder.clearDbType();
        return resultMap;
    }
    
    /**
     * 查询俱乐部下体测数据
     * @return
     */    
    @RequestMapping("/bodytest")
    public ModelAndView BodyTest(ModelAndView mav){
        //切换数据库
    	DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
    	mav.addObject("yearTermList", clubMemberInfoMapper.getAllYearTerm());
    	Long yearTermId = clubMemberInfoMapper.maxYearTermId();
    	List<Dwh> clubList = clubMemberInfoMapper.getClubByYearTermId(yearTermId);
    	mav.addObject("yearTermId", yearTermId);
    	mav.addObject("clubList", clubList);
    	mav.addObject("clubId", clubList.size()>0?clubList.get(0).getDwh():null);
    	
    	//切换数据库
    	DataSourceContextHolder.setDbType(DataSourceType.STAMINA);
    	mav.addObject("projectList", clubMemberInfoMapper.getAllProject());    	
        mav.setViewName("sports/bodytest");
        return mav;
    } 
    
	@RequestMapping("/getClubList")
    @ResponseBody
    public Object getClubList(Long yearTermId){
	    DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
		List<Dwh> clubList = clubMemberInfoMapper.getClubByYearTermId(yearTermId);
		return clubList;
	}
    /**
     * 俱乐部月活动人数
     * @param
     * @return
     */
    @RequestMapping(value = "/getMonthCountByYear", method = RequestMethod.GET)
    @ResponseBody
    public Object getMonthNumActiveCountByYear(String year,String clubName){
        DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
        Map<String, Object> resultMap = clubMemberDistService.getMonthNumActiveCountByYear(year,clubName);
        return resultMap;
    }
    
    /**
     * 俱乐部学期活动人数
     * @param
     * @return
     */
    @RequestMapping(value = "/clubYearNum", method = RequestMethod.GET)
    @ResponseBody
    public Object getclubYearNum(String year,String clubName){
        DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
        Map<String, Object> resultMap = clubMemberDistService.getClubYearNumByYear(year,clubName);
        DataSourceContextHolder.clearDbType();
        return resultMap;
    }


    /**
     * 获取每周俱乐部活动人数
     * @param clubName
     * @param year
     * @return
     */
    @RequestMapping(value = "getWeekOfYearClubPeopleNum", method = RequestMethod.GET)
    @ResponseBody
    public Object getWeekOfYearClubPeopleNum(String clubName, String year){
        ServerResponse response = clubMemberDistService.getWeekOfYearClubPeopleNum(clubName,year);
        return response;
    }
}
